import React, { Component } from 'react'
import PropTypes from 'prop-types'
import commonTypes from '../../utils/commonTypes'
import withDataList from '../../HOC/withDataList'


/**
 * 一组下拉选项
 */
class SelectOption extends Component {

    static propTypes = {
        optionItem: commonTypes.optionItem.isRequired,
    }

    render() {
        const { optionItem } = this.props
        return (
            <option value={optionItem.value}>{optionItem.label}</option>
        )
    }
}

const SelectOptionWithDataList = withDataList(SelectOption);

class Select extends Component {

    static propTypes = {
        value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
        name: PropTypes.string.isRequired,
        onChange: PropTypes.func
    }

    onChange = (e) => {
        const { onChange } = this.props;
        onChange && onChange(e.target.value);
    }

    render() {
        const { name, value } = this.props;
        return (
            <select name={name} value={value} onChange={this.onChange}>
                <SelectOptionWithDataList {...this.props} />
            </select>
        )
    }
}


export default Select